package com.reflex.jvm;

import com.reflex.jvm.model.Person;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflexJvmTest {

    private final static Integer loop = 1 * 10000 * 10000;
    public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException {
        /*long start = System.currentTimeMillis();
        for (int i = 0; i < loop; i++) {
            //Person person = Person.class.newInstance(); // 892ms
            Person person = new Person(); // 598ms
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);*/

        Person person = new Person();
        Field[] fields = person.getClass().getDeclaredFields();
        for (Field field: fields) {
            System.out.println(field);
        }
        //直接设置私有变量 的值
        Field field = person.getClass().getDeclaredField("name");
        field.setAccessible(true);
        field.set(person, "hello");
        Method method = person.getClass().getMethod("getName");
        System.out.println(person.getName());
        Object object = method.invoke(person, new String[]{});
        System.out.println(object);
    }
}
